package com.example.util.task;

import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 临时白名单异步请求处理
 */
@Slf4j
public class TemWhiteMobilesCastAsync {


    public static ExecutorService es = new ThreadPoolExecutor(20, 30, 6000L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(3000), new ThreadFactory() {
        private final AtomicInteger nextid = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable task) {
            String name = "5G_threadFactory's tempWhiteList-worker-" + nextid.getAndIncrement();
            Thread thread = new Thread(task, name);
            return thread;
        }
    }, new ThreadPoolExecutor.DiscardPolicy());

    /**
     * 批量处理临时白名单方法
     *
     * @param datas
     * @return
     */
    public static String batchProcessTempWhiteList(List<String> datas, String fileName, String fileWriterDir, String terminalManageUrl, String token) {

        List<CompletableFuture<String>> accountFindingFutureList = new ArrayList<>(3);

        // 并行处理临时白名单
        CompletableFuture<String> stringCompletableFuture = assertTempWhiteList(datas, fileName, fileWriterDir, terminalManageUrl, token);
        accountFindingFutureList.add(stringCompletableFuture);
        return null;
    }

    /**
     * 临时白名单处理任务提交，异步执行
     *
     * @param mobiles 手机号集合
     * @return
     */
    static CompletableFuture<String> assertTempWhiteList(List<String> mobiles, String fileName, String fileWriterDir, String terminalManageUrl, String token) {

        TempMobileBatchImpl batchUtil = new TempMobileBatchImpl();

        return CompletableFuture.supplyAsync(() -> {
            try {
                batchUtil.listBatchForMobile(mobiles, fileName, fileWriterDir, terminalManageUrl, token);
            } catch (Exception e) {
                log.error("临时白名单处理出现异常_" + e.getMessage(), e);
            }
            return null;
        }, es);
    }

    /**
     * 批量处理手机号后八位生成
     *
     * @param i
     * @param writeDir
     * @return
     */
    public static String batchProcessMobileNextEight(Integer i, String writeDir) {

        List<CompletableFuture<String>> accountFindingFutureList = new ArrayList<>(3);
        // 并行处理临时白名单
        CompletableFuture<String> stringCompletableFuture = assertMobileNextEight(i, writeDir);
        accountFindingFutureList.add(stringCompletableFuture);
        return null;
    }

    /**
     * 批量处理手机号后八位生成任务提交，异步执行
     *
     * @param i
     * @param writeDir 手机号集合
     * @return
     */
    static CompletableFuture<String> assertMobileNextEight(Integer i, String writeDir) {

        TempMobileBatchImpl batchUtil = new TempMobileBatchImpl();

        return CompletableFuture.supplyAsync(() -> {
            try {
                batchUtil.listBatchForMobileNextEight(i, writeDir);
            } catch (Exception e) {
                log.error("手机号后八位处理出现异常_" + e.getMessage(), e);
            }
            return null;
        }, es);
    }


}
